import request from '@/utils/request'
import { defineStore } from 'pinia'
import { IApiRes } from '@/types/data'
import { CategoryItem } from '@/types/goods'

import {topCategory} from '../constants'

const defaultCategory = topCategory.map((item) =>({
  name:item,
}))

export default defineStore('category', {
    state: () => ({
        list: defaultCategory as CategoryItem[],
    }),
    actions: {
        async getAllCategory() {
            const res = await request.get<IApiRes<CategoryItem[]>>('/home/category/head')
            res.data.result.forEach((item) => {
             item.open = false
            })
            this.list = res.data.result
        },
        show(id: string) {
            const category = this.list.find((item) => item.id === id)
            category!.open = true
        },
        hide(id: string) {
            const category = this.list.find((item) => item.id === id)
            category!.open = false
        },
    },
})